import random
import string
from basics.models import Attachment
import json


def func_check(func):
    def inner(*args, **kwargs):
        value = None
        try:
            value, status = func(*args, **kwargs)
        except Exception as e:
            status = 1
        return value, status
    return inner


@func_check
def random_int(min_int, max_int):
    # 返回范围内的整数
    # 返回min-max之间的一个随机数，包括max
    status = 0
    min_int = int(min_int)
    max_int = int(max_int)
    value = random.randint(min_int, max_int)
    return value, status


@func_check
def random_float(min_int, max_int, pla):
    '''
    返回随机小数
    :param min_int: 最小整数值
    :param max_int: 最大整数值
    :param pla: 小数位数
    :return:
    '''
    status = 0
    min_int = int(min_int)
    max_int = int(max_int)
    pla = int(pla)
    my_float = random.uniform(min_int, max_int)
    value = round(my_float, pla)
    return value, status


@func_check
def random_str(digital):
    # 返回一定数量的随机字符串(字母加数字),最多生成36个字符
    status = 0
    digital = int(digital)
    len_str = 36
    len_for = int(digital / len_str) + 1
    str_lis = []
    for i in range(len_for):
        str_lis.append(''.join(random.sample(string.ascii_lowercase + string.digits, len_str)))
    value = ''.join(str_lis)[0:digital]
    return value, status


@func_check
def random_special(digital):
    # 返回一定数量的随机字符串(特殊字符),一次最多生成32个字符
    status = 0
    digital = int(digital)
    len_str = 32
    len_for = int(digital / len_str) + 1
    str_lis = []
    for i in range(len_for):
        str_lis.append(''.join(random.sample(string.punctuation, len_str)))
    value = ''.join(str_lis)[0:digital]
    return value, status


@func_check
def random_chinese(digital):
    # 返回一定数量的中文
    status = 0
    digital = int(digital)
    str_chinese = ''.join([chr(random.randint(0x4e00, 0x9fbf)) for i in range(digital)])
    return str_chinese, status


@func_check
def get_file_paths(num, file_type, min_size, min_unit, max_size, max_unit):
    '''
    获取附件的路径
    :param num:  文件个数
    :param file_type: 文件格式
    :param size: 文件大小
    :param unit: 大小单位
    :return:
    '''
    status = 0
    num = int(num)
    min_size = int(min_size)
    max_size = int(max_size)
    unit_dict = {'bit': 0.125,
                 'Byte': 1,
                 'KB': 1024 ** 1,
                 'MB': 1024 ** 2,
                 'GB': 1024 ** 3,
                 'TB': 1024 ** 4
                 }
    min_unit = unit_dict.get(min_unit)
    max_unit = unit_dict.get(max_unit)
    query_set = Attachment.objects.filter(file_format__contains=file_type,
                                          file_size__gt=min_size * min_unit,
                                          file_size__lt=max_size * max_unit,
                                          )
    files = []
    if query_set.count() >= num:
        my_query_set = query_set[0:num]
        for dt in my_query_set:
            files.append(dt.file_data.path)
    if len(files) is not num:
        status = 1
    return files, status


@func_check
def get_file_path(file_type, min_size, min_unit, max_size, max_unit):
    '''
    获取附件的路径
    :param num:  文件个数
    :param file_type: 文件格式
    :param size: 文件大小
    :param unit: 大小单位
    :return:
    '''
    status = 0
    num = 1
    min_size = int(min_size)
    max_size = int(max_size)
    unit_dict = {'bit': 0.125,
                 'Byte': 1,
                 'KB': 1024 ** 1,
                 'MB': 1024 ** 2,
                 'GB': 1024 ** 3,
                 'TB': 1024 ** 4
                 }
    min_unit = unit_dict.get(min_unit)
    max_unit = unit_dict.get(max_unit)
    query_set = Attachment.objects.filter(file_format__contains=file_type,
                                          file_size__gt=min_size * min_unit,
                                          file_size__lt=max_size * max_unit,
                                          )
    files = []
    if query_set.count() >= num:
        my_query_set = query_set[0:num]
        for dt in my_query_set:
            files.append(dt.file_data.path)
    file_path = ""
    if len(files) is not num:
        status = 1
    else:
        file_path = json.dumps(files).strip('[""]')
    return file_path, status


if __name__ == '__main__':
    a = -10
    b = 10
    # aa = random_int(a,b)
    # aa = random_float(a,b,2)
    # aa = random_str(100000)
    # aa = random_chinese(2000)
    aa = random_special(32000)
    print(aa)
    # get_file_path(2, 'txt', 2, 5, 'MB')
